package com.gitee.wsl.jvm.store.config

import com.gitee.wsl.data.store.AbstractReadOnlyStoreServer
import com.typesafe.config.Config
import kotlin.reflect.KClass

class ConfigStoreServer(val preferences: Config): AbstractReadOnlyStoreServer() {

    override fun <T:Any> getOrNull(key: String, type: KClass<T>): T? {
        //if(type == null) throw IllegalArgumentException("This type can be get from Preferences")

        if(!preferences.hasPath(key)) return null

        val value: Any = when(type) {
                Long -> preferences.getLong(key)
                String -> preferences.getString(key)
                Int -> preferences.getInt(key)
                Boolean -> preferences.getBoolean(key)
                Float -> preferences.getDouble(key).toFloat()
                Double-> preferences.getDouble(key)
                else -> {
                     return null
                     //getExtPreference(key, type)
                }
        }

        return value as T
    }

    override fun isExits(key: String): Boolean {
        return preferences.hasPathOrNull(key)
    }

    companion object
}